package com.hmkj.admin.controller.wx;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.JsonUtils;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.wx.WxClickReply;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.wx.WxClickReplyService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 微信点击回复素材相关Controller
 *
 * @author Vector
 * @eate 2017-06-15 17:42
 */
@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class WxClickReplyJsonController extends GenericController<Integer, WxClickReply> {

    @Resource
    WxClickReplyService wxClickReplyService;
    @Resource
    RedisService redisService;

    /**
     * 列表JSON数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "clickreply/json/list", method = RequestMethod.POST)
    public DtGrid clickReplyListJson(String dtGridPager) throws Exception {
        return wxClickReplyService.getDtGridList(dtGridPager);
    }

    /**
     * 新增点击回复素材
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "clickreply/json/add", method = RequestMethod.POST)
    public ResultEntity clickReplyAdd(@RequestParam(value = "title") String title,
                                      @RequestParam(value = "note") String note,
                                      @RequestParam(value = "code") String code,
                                      @RequestParam(value = "type") Integer type,
                                      @RequestParam(value = "content") String content,
                                      @RequestParam(value = "picTitle") String picTitle,
                                      @RequestParam(value = "picDesc") String picDesc,
                                      @RequestParam(value = "picImage") String picImage,
                                      @RequestParam(value = "picClickUrl") String picClickUrl) {
        WxClickReply wxClickReply = new WxClickReply();
        wxClickReply.setTitle(title);
        wxClickReply.setNote(note);
        wxClickReply.setCode(code);
        wxClickReply.setType(type);
        if (type == 1) {
            wxClickReply.setContent(content);
        } else {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("title", picTitle);
            hashMap.put("desc", picDesc);
            hashMap.put("picurl", picImage);
            hashMap.put("url", picClickUrl);
            String jsonContent = JsonUtils.toJson(hashMap);
            wxClickReply.setContent(jsonContent);
        }
        wxClickReplyService.insert(wxClickReply);

        ResultEntity resultEntity = new ResultEntity();
        // 定义返回结果对象
        resultEntity.setUrl(redisService.get(ConfigNID.ADMIN_SERVER_URL) + "weixin/list");
        resultEntity.setMessage("添加成功");
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }


    /**
     * 删除记录
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "clickreply/json/del", method = RequestMethod.POST)
    public ResultEntity clickReplyDel(@RequestParam(value = "id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        if (id <= 0) {
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("参数错误");
            return resultEntity;
        }
        try {
            wxClickReplyService.deleteByPrimaryKey(id);
            resultEntity.setCode(ResultEntity.SUCCESS);
            resultEntity.setMessage("删除成功");
            return resultEntity;
        } catch (BussinessException e) {
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("删除失败");
            return resultEntity;
        }
    }

    /**
     * 编辑点击回复素材
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "clickreply/json/save", method = RequestMethod.POST)
    public ResultEntity clickReplySave(@RequestParam(value = "id") Integer id,
                                       @RequestParam(value = "title") String title,
                                       @RequestParam(value = "note") String note,
                                       @RequestParam(value = "code") String code,
                                       @RequestParam(value = "editType") Integer editType,
                                       @RequestParam(value = "content") String content,
                                       @RequestParam(value = "picTitle") String picTitle,
                                       @RequestParam(value = "picDesc") String picDesc,
                                       @RequestParam(value = "picImage") String picImage,
                                       @RequestParam(value = "picClickUrl") String picClickUrl) {
        ResultEntity resultEntity = new ResultEntity();
        if (id <= 0) {
            resultEntity.setCode(ResultEntity.FAIL);
            resultEntity.setMessage("参数错误");
            return resultEntity;
        }
        WxClickReply wxClickReply = new WxClickReply();
        wxClickReply.setId(id);
        wxClickReply.setTitle(title);
        wxClickReply.setNote(note);
        wxClickReply.setCode(code);
        wxClickReply.setType(editType);
        if (editType == 1) {
            wxClickReply.setContent(content);
        } else {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("title", picTitle);
            hashMap.put("desc", picDesc);
            hashMap.put("picurl", picImage);
            hashMap.put("url", picClickUrl);
            String jsonContent = JsonUtils.toJson(hashMap);
            wxClickReply.setContent(jsonContent);
        }
        wxClickReplyService.updateByPrimaryKeySelective(wxClickReply);
        // 定义返回结果对象
        resultEntity.setUrl(redisService.get(ConfigNID.ADMIN_SERVER_URL) + "weixin/clickreplylist");
        resultEntity.setMessage("编辑成功");
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }

    /**
     * 反序列化图文回复内容数据
     */
    @ResponseBody
    @RequestMapping(value = "clickreply/json/getcontent", method = RequestMethod.POST)
    public ResultEntity getPicContent(@RequestParam(value = "content") String content) {
        ResultEntity resultEntity = new ResultEntity();
        HashMap<String, String> result = (HashMap<String, String>) JsonUtils.toObject(content, HashMap.class);
        resultEntity.setData(result);
        resultEntity.setCode(ResultEntity.SUCCESS);
        return resultEntity;
    }
}
